<template>
  <view class="detail-container" v-if="goods_info.goods_price">
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular>
      <swiper-item v-for="(item,index) in goods_info.pics" :key="item.pics_id">
        <image :src="item.pics_big" mode="widthFix" @click="preview(index)"></image>
      </swiper-item>
    </swiper>
    <view class="goods_info_box">
      <view class="goods-price">
        ￥ {{goods_info.goods_price}}
      </view>
      <view class="goods_info">
        <view class="goods_name">
          {{goods_info.goods_name}}
        </view>
        <!-- 收藏 -->
        <view class="goods-fav">
          <uni-icons type="star" size="24" color="gray"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <view class="yf">
        快递：免运费
      </view>
    </view>
    <rich-text :nodes="goods_info.goods_introduce"></rich-text>

    <view class="goods_nav">
      <uni-goods-nav :fill="true" :options="options" :buttonGroup="buttonGroup" @click="onClick"
        @buttonClick="buttonClick" />
    </view>
  </view>
</template>

<script>
  import {
    mapState,
    mapMutations,
    mapGetters
  } from 'vuex'

  export default {
    watch: {
      total: {
        handler(newValue) {
          const result = this.options.find(x => x.text === '购物车')
          if (result) {
            result.info = newValue
          }
        },
        immediate: true
      }
    },
    computed: {
      ...mapState('m_cart', []),
      ...mapGetters('m_cart', ['total'])
    },
    data() {
      return {
        goods_info: {},
        options: [{
          icon: 'headphones',
          text: '客服'
        }, {
          icon: 'shop',
          text: '店铺',
          info: 0,
          infoBackgroundColor: '#007aff',
          infoColor: "white"
        }, {
          icon: 'cart',
          text: '购物车',
          info: 0
        }],
        buttonGroup: [{
            text: '加入购物车',
            backgroundColor: '#ff0000',
            color: '#fff'
          },
          {
            text: '立即购买',
            backgroundColor: '#ffa200',
            color: '#fff'
          }
        ]

      }
    },
    methods: {
      async getGoodsDetail(goods_id) {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/goods/detail', {
          goods_id
        })
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }

        //添加display:block样式解决图片之间的空白间隙问题
        //ios设备对webp格式的图片支持得不是很好，会显示不出来
        res.message.goods_introduce = res.message.goods_introduce.replace(/<img/g, '<img style="display:block;" ')
          .replace(/webp/g, "jpg");

        this.goods_info = res.message;
      },
      preview(index) {
        uni.previewImage({
          urls: this.goods_info.pics.map(item => item.pics_big),
          current: index
        })
      },
      onClick(e) {
        console.log(e)
        if (e.content.text === '购物车') {
          uni.switchTab({
            url: '/pages/cart/cart'
          })
        }
      },
      buttonClick(e) {
        console.log(e)
        if (e.content.text === '加入购物车') {
          const goods = {
            goods_id: this.goods_info.goods_id, // 商品的Id
            goods_name: this.goods_info.goods_name, // 商品的名称
            goods_price: this.goods_info.goods_price, // 商品的价格
            goods_count: 1, // 商品的数量
            goods_small_logo: this.goods_info.goods_small_logo, // 商品的图片
            goods_state: true // 商品的勾选状态
          }
          this.addToCart(goods)
        }
      },

      ...mapMutations('m_cart', ['addToCart']),
    },
    onLoad(options) {
      this.getGoodsDetail(options.goods_id)
    }
  }
</script>

<style lang="scss">
  .detail-container {
    padding-bottom: 90rpx;
  }

  swiper {
    height: 360px;

    image {
      width: 100%;
    }
  }

  .goods_info_box {
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    background-color: #fff;
    padding: 30rpx 20rpx;

    .goods-price {
      color: #c00000;
      font-size: 48rpx;
      margin-bottom: 10rpx;
    }

    .goods_info {
      display: flex;
      justify-content: space-between;
      font-size: 30rpx;

      .goods_name {
        border-right: #efefef solid 5rpx;
        padding-right: 20rpx;
      }

      .goods-fav {
        width: 280rpx;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        color: gray;
      }
    }

    .yf {
      font-size: 24rpx;
      margin-top: 10rpx;
      color: gray;
    }
  }

  .goods_nav {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
  }
</style>
